
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )


function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "neodymium" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end


/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
	self.Entity:SetMaterial("models/shiny")
	self.Entity:SetColor(0, 0, 0, 255 )
	self.Entity:PhysicsInit( SOLID_VPHYSICS ) // Make us work with physics,
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS ) // after all, gmod is a physics
	self.Entity:SetSolid( SOLID_VPHYSICS ) 
	self.Entity:PhysicsInitSphere( 15, "gmod_bouncy" )
	self.Entity:SetCollisionGroup(COLLISION_GROUP_PROJECTILE)
	
	local phys = self.Entity:GetPhysicsObject()

	
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMaterial("gmod_bouncy")
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
		
end



/*---------------------------------------------------------
   Name: PhysicsCollide
---------------------------------------------------------*/
function ENT:PhysicsCollide( data, physobj )
	local effectdata = EffectData()
	effectdata:SetMagnitude(25)
	effectdata:SetEntity(self.Entity)
	util.Effect("TeslaHitBoxes",effectdata)  
	if ValidEntity(data.HitEntity) then
		effectdata:SetMagnitude(25)
		effectdata:SetEntity(data.HitEntity)
	end
end

/*---------------------------------------------------------
   Name: OnTakeDamage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )
	
end


/*---------------------------------------------------------
   Name: Use
---------------------------------------------------------*/
function ENT:Use( activator, caller )
end

function ENT:Think()
	local pos = self.Entity:GetPos()
	local radius = 8000
	local tab = ents.FindInSphere(pos,radius)
	for k,v in pairs(tab) do
		local dist = v:GetPos():Distance(pos)
		local newpos = v:GetPos() - pos
		local phys = v:GetPhysicsObject()
		if (phys:IsValid() and ValidEntity(v)) and (!v:IsPlayer() and !v:IsNPC()) then
			phys:ApplyForceCenter(((8*phys:GetMass() / ((newpos:Length() + 1) / (radius+20))) * newpos:GetNormal())*-1)
			if dist < 400 then
				if constraint.HasConstraints(v) then
					constraint.RemoveAll(v)
				end
			end
			if v:GetPhysicsObject():IsValid() and !v == self.Ent then 
				if dist < 60 then
					v:GetPhysicsObject():EnableMotion(false)
				elseif dist > 60 and dist < 400 then
					v:GetPhysicsObject():EnableMotion(true)
				end
			end
		elseif ((v:IsPlayer() and v:GetMoveType() != MOVETYPE_NOCLIP) or v:IsNPC()) then
			v:SetVelocity(((25 / ((newpos:Length() + 1) / (radius+20))) * newpos:GetNormal())*-1)
		end
	end
end
